package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.itheima.ydd.config.R;
import com.itheima.ydd.dto.SysMenuDto;
import com.itheima.ydd.entity.SysMenu;
import com.itheima.ydd.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/sys_menu")
public class SysMenuController {

    @Autowired
    private ISysMenuService sysMenuService;

    /**
     *
     * 获取所有权限
     * @return
     */
    @GetMapping("/allData")
    public R<List> allData(){
       List<SysMenuDto>sysMenuDtos= sysMenuService.getAllData();

       return R.success(sysMenuDtos);
    }
    @GetMapping("/rowInfo")
    public R<SysMenu>rowInfo(int id){

        LambdaQueryWrapper<SysMenu> sysMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysMenuLambdaQueryWrapper.eq(SysMenu::getId,id);
        SysMenu one = sysMenuService.getOne(sysMenuLambdaQueryWrapper);
        return R.success(one);
    }



    @PostMapping("/update")
    public R<String>update(@RequestBody SysMenu sysMenu){

        sysMenuService.updateById(sysMenu);

        return R.success("修改成功");

    }

    @GetMapping("/del")
    public R<String>del(int id){

        sysMenuService.removeById(id);

        return R.success("删除成功");

    }

    @PostMapping("/add")
    public R<String>add(@RequestBody SysMenu sysMenu){
        sysMenu.setCreateDate(LocalDateTime.now());
        sysMenuService.save(sysMenu);
        return R.success("新增成功");
    }
}
